import ArrayQueue from "./01_ArrayQueue";

function hotPotato(names: string[], num: number) {
  const queue = new ArrayQueue();
  names.forEach((name) => queue.enqueue(name));

  while (queue.size > 1) {
    // 第一个人不需要加入到队尾，从第二个人开始加入到队尾
    for (let i = 1; i < num; i++) {
      queue.enqueue(queue.dequeue());
    }

    queue.dequeue();
  }

  return {
    name: queue.front(),
    index: names.indexOf(queue.front() as string),
  };
}

// passGame() 测试
const names = ["lily", "lucy", "tom", "tony", "jack"];
const obj = hotPotato(names, 4);
console.log("击鼓传花", obj.name, obj.index); //--> lily
